// pages/index/index.js
const app = getApp();
const url = app.globalData.url;
const common = require("../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iszd:1,
    url : app.globalData.url,
    toplt:[],
    jzs:false,
    xlsx:false,
    bqs:true,
    jzwz:'数据已加载完毕',
    loding:true,
    banner:[],
    zdlist:[],
    zxlist:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.peizi();
  },
  tourl(e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  tozd(e) {
    let iszd = e.currentTarget.dataset.zd;
    this.setData({iszd:iszd});
    this.hqlist();
  },
   // 获取系统消息
   getxtxx(){
    let that = this;
    let user = that.data.user;
    app.fetch('api/wx/getxtxx', { uid: user.uid, gl: user.isqx == 2 || user.isqx == 3 ? 'gl' : '' }, "POST", 
    (err, data) => {
      console.log(data);
      that.dinshi();
      that.setData({ "daohang.xx": data.recordCount - user.xtxx });
    })
  },
  // 定时器
  dinshi: function () {
    this.loding_timer = setTimeout(() => {
      this.getxtxx();
    }, 10 * 1000)
  },
  // 跳转搜索
  tosearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  // 轮播图
  getbanner() {
    let that = this;
    let sj = {
      pgs: 8
    };
    app.fetch('api/wx/getbanner', sj, "POST", (err, data) => {
      console.log(data)
      for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].img) {
          data.data[i].img = url + data.data[i].img;
        }
      }
      that.setData({
        banner: data.data,
      });
    })
  },
  // 导航
  getdaohang() {
    let that = this;
    let pages = getCurrentPages();
    let currPage = null;
    if (pages.length) {
      currPage = "/" + (pages[pages.length - 1]).route;
    }
    let pz = that.data.peizi.xcxdh;
    console.log(currPage)
    app.fetch('api/wx/getdh', { pgs: 5, lx: 1, isxs: 1 }, "GET", (err, data) => {
      console.log(data)
      for (var i = 0; i < data.data.length; i++) {
        data.data[i].img = url + data.data[i].img;
        data.data[i].cimg = url + data.data[i].cimg;
        if (data.data[i].tzurl == currPage) {
          that.setData({ "daohang.show": true });
          wx.hideHomeButton();
        }
      }
      that.setData({
        "daohang.dhlist": data.data,
        "daohang.recordCount": data.recordCount,
        "daohang.ckpage": currPage,
        "daohang.color": pz.color.txt,
        "daohang.ccolor": pz.ccolor.txt,
        "daohang.background": pz.background.txt,
      });
    })
  },
  // 获取top导航
  gettopdh(){
    let that = this;
    let sj={
      fldj:1, 
    };
    app.fetch('api/wx/getdh', { pgs: 8, lx: 2, isxs: 1 }, "GET", (err, data) => {
      console.log(data)
      for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].img){
          data.data[i].img = url + data.data[i].img;
        }
      }
      that.setData({toplt:data.data});
    })
  },
  // 配置文件
  peizi: function () {
    let that = this;
    app.fetch('api/wx/getpeizi', { dlx: 'xcxindex' }, "GET", (err, data) => {
      console.log(data)
      if (data.code == 200) {
        wx.setNavigationBarTitle({
          title: data.data.xcxindex.title.txt
        });
        data.data.xcxindex.zttp.txt=url+data.data.xcxindex.zttp.txt;
        that.setData({peizi:data.data});
        that.hquser();
        that.getbanner();
        that.getdaohang();
        that.gettopdh();
        if (data.data.xcxindex.syxslx.txt == 2){
          that.hqlist();
        }else{
          that.hqlist2();
          that.hqlist3();
        }
        if(data.data.xcxindex.wzxs.txt==1){
          that.hqwzlist();
        }
      } else {
        wx.showModal({
          title: '接口数据错误',
          content: '接口:"' + url + 'api/wx/getpeizi.php' + '",msg:' + data.msg,
          showCancel: false,//是否显示取消按钮
          confirmText: "好的",//默认是“确定”
          confirmColor: 'skyblue',//确定文字的颜色
          success: function (res) {

          },
          fail: function (res) { },//接口调用失败的回调函数
          complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
        })
      }
    })
  },
  // 获取user信息;
  hquser() {
    let that = this;
    wx.getStorage({
      key: 'openid',
      success(res) {
        app.fetch('api/wx/user', { openid: res.data }, "POST", (err, data) => {
          if (data.code == 200) {
            that.setData({ user: data.data[0] })
            that.getxtxx();
          } 
        })
      }
    })
  },
  //获取列表
  hqwzlist() {
    let that = this;
    let list = []
    let pno = 1;
    let sj = {
      pno:pno,
      pgs:10
    };
    app.fetch('api/wx/getwzlist', sj, "POST", (err, data) => {
      console.log(data)
      if (data.code == 200) {
        for (var i = 0; i < data.list.length; i++) {
          if (data.list[i].fmtp) {
            data.list[i].fmtp = url + data.list[i].fmtp;
          }
          data.list[i].addtime = common.bijiao(data.list[i].addtime);

          list.push(data.list[i]);
        }
      }
      that.setData({
        wzlist: list,
      });
    })
  },
  // 没有登录
  login(){
    wx.showModal({
      title: '提示',
      content: '您需要先登录,才能发表内容',
      showCancel: true,//是否显示取消按钮
      cancelText: "先看看",//默认是“取消”
      // cancelColor: '#f',//取消文字的颜色
      confirmText: "去登陆",//默认是“确定”
      // confirmColor: '',//确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
          // if (options.finish) {
          // wx.navigateBack({ delta: 1 })
          // }
        } else {
          wx.redirectTo({
            url: '/pages/my/my',
          })
        }
      },
      fail: function (res) { },//接口调用失败的回调函数
      complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  //获取列表
  hqlist(sx){
    let that = this;
    let list = []
    let pno = 1;
    let sq = '';
    if (sx) {
      list = that.data.list;
      pno = that.data.pno;
      if (that.data.iszd== '1') {
        for (let p of list) {
          sq += ' AND did!=' + p.did;
        }
        pno = 1;
      }
    }
    let sj = {
      pno: pno,
      pgs: 10,
      sq: sq,
      iszd:that.data.iszd
    };
    app.fetch('api/wx/getindex', sj, "POST", (err, data) => {
      console.log(data)
      for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].ewm) {
          data.data[i].ewm = url + data.data[i].ewm;
        }
        data.data[i].addtime = common.bijiao(data.data[i].addtime);
        if (data.data[i].logolist) {
          var arr = data.data[i].logolist.split(",");
          data.data[i].logolist = [];
          let ls = arr.length < 5 ? arr.length : 5;
          for (let a = 0; a < ls; a++) {
            data.data[i].logolist.push(url +arr[a]);
          }
        } else {
          data.data[i].logolist = [];
          // data[i].imgs.push(data[i].tx);
        };
        list.push(data.data[i]);
        wx.hideLoading();
      }
      that.setData({
        list: list,
        pno: Number(data.pno) + 1,
        pcount: Number(data.pageCount), 
        loding: false 
      });
      if (that.data.xlsx) {
        wx.stopPullDownRefresh() //停止下拉刷新
        wx.hideNavigationBarLoading() //完成停止加载
      }
    })
  },
  hqlist2(){
    let that = this;
    let sj = {
      pno: 1,
      pgs: 5,
      iszd: 1
    };
    app.fetch('api/wx/getindex', sj, "POST", (err, data) => {
      console.log(data)
      for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].ewm) {
          data.data[i].ewm = url + data.data[i].ewm;
        }
        data.data[i].addtime = common.bijiao(data.data[i].addtime);
        if (data.data[i].logolist) {
          var arr = data.data[i].logolist.split(",");
          data.data[i].logolist = [];
          let ls = arr.length < 5 ? arr.length : 5;
          for (let a = 0; a < ls; a++) {
            data.data[i].logolist.push(url + arr[a]);
          }
        } else {
          data.data[i].logolist = [];
          // data[i].imgs.push(data[i].tx);
        };
      }
      that.setData({
        zdlist: data.data,
        loding: false
      });
    })
  },
  hqlist3(){
    let that = this;
    let sj = {
      pno: 1,
      pgs: 5,
      iszd: 2
    };
    app.fetch('api/wx/getindex', sj, "POST", (err, data) => {
      console.log(data)
      for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].ewm) {
          data.data[i].ewm = url + data.data[i].ewm;
        }
        data.data[i].addtime = common.bijiao(data.data[i].addtime);
        if (data.data[i].logolist) {
          var arr = data.data[i].logolist.split(",");
          data.data[i].logolist = [];
          let ls = arr.length < 5 ? arr.length : 5;
          for (let a = 0; a < ls; a++) {
            data.data[i].logolist.push(url + arr[a]);
          }
        } else {
          data.data[i].logolist = [];
          // data[i].imgs.push(data[i].tx);
        };
      }
      that.setData({
        zxlist: data.data,
        loding: false
      });
      if(that.data.xlsx){
        wx.stopPullDownRefresh() //停止下拉刷新
        wx.hideNavigationBarLoading() //完成停止加载
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //  this.dinshi();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("关闭定时器")
    clearTimeout(this.loding_timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("关闭定时器")
    clearTimeout(this.loding_timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({ xlsx:true})
    this.peizi();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.peizi.xcxindex.syxslx.txt == 2) {
      var count = that.data.pcount;
      var pno = that.data.pno;
      console.log(pno, count)
      if (pno - 1 < count) {
        wx.showLoading({
          title: "正在加载中....",
          // mask: true
        });
        that.hqlist('sx');
      } else {
        that.setData({ jzs: true });
        setTimeout(function () {
          that.setData({ jzs: false });
        }, 1000)
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onShareTimeline(){
    let that=this;
    return {
      title:that.data.peizi.xcx.title.txt,
      query:"/pages/index/index"
    }
  }
})